package br.com.puertorico.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;

public class PasswordUtils {

	public static final char[] AVALIABLE_CHARS = { 'a', 'b', 'c', 'd', 'e',
			'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
			's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
			'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
			'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

	public static final char[] NUMBER_CHARS = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9' };

	public static final char[] SECURITY_CHARS = { '?', '!', '#', '%', '@', '$',
			'*' };

	/**
	 * Generates an eight characters long password consisting of hexadecimal
	 * characters.
	 *
	 * @return the generated password
	 */
	public static String generate() {
		StringBuilder passwd = new StringBuilder();
		passwd.append(RandomStringUtils.random(3, AVALIABLE_CHARS));
		passwd.append(RandomStringUtils.random(1, SECURITY_CHARS));
		passwd.append(RandomStringUtils.random(3, AVALIABLE_CHARS));
		passwd.append(RandomStringUtils.random(1, SECURITY_CHARS));
		passwd.append(RandomStringUtils.random(2, NUMBER_CHARS));
		return passwd.toString();
	}

	public static String encryptMD5(String password, boolean base64) {
		String passwd = DigestUtils.md5Hex(password);
		if (base64) {
			return new String(Base64.encodeBase64(DigestUtils.md5(passwd)));
		}
		return passwd;
	}

}
